/**
 * components util
 */

/**
 * 清理空值，对象
 * @param children
 * @returns {*[]}
 */
export function filterEmpty (children = []) {
  return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
}
/**
 * 花名册判断页面数据是否改变
 * @param children
 * @returns {*[]}
 */
export function objectIsChange(oldModel,newModel) {
  const isEqure=JSON.stringify(oldModel)===JSON.stringify(newModel)     
  return isEqure
}

/**
 * 花名册根据入职时间计算工龄
 * @param date
 */
export function calculationWorkAge(date,endDate) {
  var workdate = new Date(date.replace(/-/g, '/')) //开始时间
  // var nowdate = new Date() //结束时间
  var nowdate =endDate?new Date(endDate.replace(/-/g, '/')):new Date()
  if (nowdate.getTime() < workdate.getTime()) {
    return false
  }
  var Y1 = nowdate.getFullYear() // 结束年份
  var Y2 = workdate.getFullYear() // 开始年份
  var M1 = nowdate.getMonth() + 1 // 结束月份
  var M2 = workdate.getMonth() + 1 // 开始月份
  var D1 = nowdate.getDate() // 结束日期
  var D2 = workdate.getDate() // 开始日期
  var M1_days = new Date(Y1, M1, 0).getDate() // 结束时间当月的天数
  var M2_days = new Date(Y2, M2, 0).getDate() // 开始时间当月的天数
  var obj = {} // 结果存储器
  var year = Y1 - Y2 - 1 // 相差整年数
  var month = M1 - 1 + 12 - M2 // 相差整月数(非同年);
  if (year < 0) {
    month = M1 - M2 - 1  // 相差整月数(同年)
  }
  var diffDays = D1 + (M2_days - D2) // 相差总天数
  if (diffDays >= M2_days) {
    month = month + 1
    var day = diffDays - M2_days
  } else {
    var day = diffDays
  }
  year += Math.floor(month / 12)
  if (year < 0) {
    year = 0
  }
  month = month % 12;
  obj.year = year;
  obj.month = month;
  obj.day = day;
  let time = ''
  // 1年0月2天
  if (year > 0 && month == 0 && day == 0) {
    time = obj.year + '年'
  } else if (year > 0 && month > 0 && day > 0) {
    time = obj.year + '年' + obj.month + '个月' + obj.day + '天'
  }else if (year > 0 && month > 0 && day == 0) {
    time = obj.year + '年' + obj.month + '个月'
  } else if (year > 0 && month == 0 && day > 0) {
    time = obj.year + '年' + obj.day + '天'
  } else if (year == 0 && month > 0 && day > 0) {
    time = obj.month + '个月' + obj.day + '天'
  } else if (year == 0 && month > 0 && day == 0) {
    time = obj.month + '个月'
  } else if (year == 0 && month == 0 && day >= 0) {
    time = obj.day + '天'
  }

  return time;
}
// 数字金额转化成人民币的中文
export function numberToChinese(money) {
  //汉字的数字
  var cnNums = new Array(
    "零",
    "壹",
    "贰",
    "叁",
    "肆",
    "伍",
    "陆",
    "柒",
    "捌",
    "玖"
  );
  //基本单位
  var cnIntRadice = new Array("", "拾", "佰", "仟");
  //对应整数部分扩展单位
  var cnIntUnits = new Array("", "万", "亿", "兆");
  //对应小数部分单位
  var cnDecUnits = new Array("角", "分", "毫", "厘");
  //整数金额时后面跟的字符
  var cnInteger = "整";
  //整型完以后的单位
  var cnIntLast = "元";
  //最大处理的数字
  var maxNum = 999999999999999.9999;
  //金额整数部分
  var integerNum;
  //金额小数部分
  var decimalNum;
  //输出的中文金额字符串
  var chineseStr = "";
  //分离金额后用的数组，预定义
  var parts;
  if (money == "") {
    return "";
  }
  money = parseFloat(money);
  if (money >= maxNum) {
    //超出最大处理数字
    return "";
  }
  if (money == 0) {
    chineseStr = cnNums[0] + cnIntLast + cnInteger;
    return chineseStr;
  }
  //转换为字符串
  money = money.toString();
  if (money.indexOf(".") == -1) {
    integerNum = money;
    decimalNum = "";
  } else {
    parts = money.split(".");
    integerNum = parts[0];
    decimalNum = parts[1].substr(0, 4);
  }
  //获取整型部分转换
  if (parseInt(integerNum, 10) > 0) {
    var zeroCount = 0;
    var IntLen = integerNum.length;
    for (var i = 0; i < IntLen; i++) {
      var n = integerNum.substr(i, 1);
      var p = IntLen - i - 1;
      var q = p / 4;
      var m = p % 4;
      if (n == "0") {
        zeroCount++;
      } else {
        if (zeroCount > 0) {
          chineseStr += cnNums[0];
        }
        //归零
        zeroCount = 0;
        chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
      }
      if (m == 0 && zeroCount < 4) {
        chineseStr += cnIntUnits[q];
      }
    }
    chineseStr += cnIntLast;
  }
  //小数部分
  if (decimalNum != "") {
    var decLen = decimalNum.length;
    for (var i = 0; i < decLen; i++) {
      var n = decimalNum.substr(i, 1);
      if (n != "0") {
        chineseStr += cnNums[Number(n)] + cnDecUnits[i];
      }
    }
  }
  if (chineseStr == "") {
    chineseStr += cnNums[0] + cnIntLast + cnInteger;
  } else if (decimalNum == "") {
    chineseStr += cnInteger;
  }
  return chineseStr;
}
//累加计算
export function accAdd(arg1, arg2) {
  let r1, r2, m, n
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  n = r1 >= r2 ? r1 : r2
  return +((arg1 * m + arg2 * m) / m).toFixed(n)
}

/**
* 判断对象的值是不是全为空
*/
export function objectValueAllEmpty(object) {
  var isEmpty = true;
  Object.keys(object).forEach(function (x) {
    if (object[x] != null && object[x] != "") {
      isEmpty = false;
    }
  });
  return isEmpty;
}

/*
 *数组对象去重
 */
export function arrRedo(arr, batch) {
  if (!Array.isArray(arr)) {
    return arr;
  }
  if (arr.length == 0) {
    return [];
  }
  let obj = {};
  let uniqueArr = arr.reduce(function (total, item) {
    obj[item[batch]] ? '' : (obj[item[batch]] = true && total.push(item));
    return total;
  }, []);
  return uniqueArr;
}